﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using System.Collections;
using ECCIT;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using System.Data;
using System.IO;

public partial class admin_UKNOW_element_element_add :AdminBasePage
{
    private const string VIEWSTATE_COLUMN_ID = "columnid";
    private const string VIEWSTATE_LAYER_ID = "layerid";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState[VIEWSTATE_COLUMN_ID] = int.Parse(Request.Params[VIEWSTATE_COLUMN_ID]);
            ViewState[VIEWSTATE_LAYER_ID] = Request.Params[VIEWSTATE_LAYER_ID];
            infoname.Text = "新增活动";

            //获取字典里的活动城市
            DataSet ds = new DictionaryManageBLL().GetDictionary(" upid = '393'");
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                DataTable table = ds.Tables[i];
                foreach (DataRow row in table.Rows)
                {
                    this.area_ddl.Items.Add(new ListItem(row["cnname"].ToString(), row["CID"].ToString()));
                }
            }
            //获取 活动的艺术形式
            IList<ArtFormModel> Artlist = new ArtFormBLL(LangID).SelectArtForms();
            foreach (ArtFormModel art in Artlist)
            {
                this.art_ddl.Items.Add(new ListItem(art.Name, art.ID.ToString()));
            }
            //获取赞助商
            IList<SponsorModel> Sponsorlist = new SponsorBLL(LangID).selectSponsor();
            foreach (SponsorModel spon in Sponsorlist)
            {
                this.sponsors_ddl.Items.Add(new ListItem(spon.Name, spon.ID.ToString()));
            }
            //初始模板
            ModelManageBLL mmb = new ModelManageBLL();
            DataSet temdata = mmb.GetModel("");
            for (int i = 0; i < temdata.Tables.Count; i++)
            {
                DataTable table = temdata.Tables[i];
                for (int j = 0; j < table.Rows.Count; j++)
                {
                    DataRow row = table.Rows[j];

                    this.templateId.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));

                }
            }
        }
    }
    protected void Sub_click(object sender, EventArgs e)
    {
        EventsBLL bll = new EventsBLL(this.LangID);
        EventsModel info = new EventsModel();

        string filename = "";
        string topImgName = "";
        string path = Server.MapPath("~/uploadfiles/image/");
        //列表图上传
        if (file.PostedFile.ContentLength > 0)
        {
            string name = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ext = file.FileName.Substring(file.FileName.LastIndexOf("."), file.FileName.Length - (file.FileName.LastIndexOf(".")));
            filename = name + ext;
            if (File.Exists(path + filename))
            {
                File.Delete(path + filename);
            }
            else
            {
                file.SaveAs(path + filename);
            }
        }
        //置顶图上传
        if (filetop.PostedFile.ContentLength > 0)
        {
            string ext = filetop.FileName.Substring(filetop.FileName.LastIndexOf("."), filetop.FileName.Length - (filetop.FileName.LastIndexOf(".")));
            string topname = DateTime.Now.ToString("yyyyMMddHHmmss") + filetop.PostedFile.ContentLength.ToString();
            topImgName = topname + ext;
            if (File.Exists(path + topImgName))
            {
                File.Delete(path + topImgName);
            }
            else
            {
                filetop.SaveAs(path + topImgName);
            }
        }
        //info.Title = title.Text;
        info.LanguageID = LangID;
        info.City = int.Parse(area_ddl.SelectedValue);
        info.Venue = address_txt.Text;
        info.ArtFormID = int.Parse(art_ddl.SelectedValue);
        info.StartTime = DateTime.Parse(time_txt.Text);
        info.SponsorID = int.Parse(sponsors_ddl.SelectedValue);
        info.ShortDescription = summary_txt.Text;
        info.Detail = content.Text;
        info.IsHot = hot_rbt.SelectedItem.Selected;
        info.IsRecommendation = recom_rbt.SelectedItem.Selected;
        info.IsHomepageTop = indextop.SelectedItem.Selected;
        info.IsColumnTop = columntop.SelectedItem.Selected;
        info.ColumnID = int.Parse(ViewState[VIEWSTATE_COLUMN_ID].ToString());
        info.LayerIDs = ViewState[VIEWSTATE_LAYER_ID].ToString();
        info.State = int.Parse(pub.SelectedValue);
        info.ListImg = filename;
        info.TopImg = topImgName;
        ResultInfo res = bll.Save(info);
        if (res.Result)
        {
            Response.Redirect("event_list.aspx?columnid=" + ViewState[VIEWSTATE_COLUMN_ID] + "&layerid=" + ViewState[VIEWSTATE_LAYER_ID] + "&langID" + LangID);
        }
        else
        {
            Response.Write("<script>alert('保存失败')</script>");
            Response.Write(res.Message);
        }

    }
}
